package org.dromara.file.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.minio.GetObjectArgs;
import io.minio.GetObjectResponse;
import io.minio.MinioClient;
import io.minio.errors.*;
import io.minio.messages.Bucket;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.config.MinioConfig;
import org.dromara.common.core.config.RuoYiConfig;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.file.domain.SysMediaFiles;
import org.dromara.file.domain.bo.SysMediaFilesBo;
import org.dromara.file.domain.vo.SysMediaFilesVo;
import org.dromara.file.service.ISysMediaFilesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * 文件管理
 *
 * @author Gjb
 * @date 2025-06-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/mediaFiles")
public class SysMediaFilesController extends BaseController {

    private final ISysMediaFilesService sysMediaFilesService;

    /**
     * 查询文件管理列表
     */
    @SaCheckPermission("system:mediaFiles:list")
    @GetMapping("/list")
    public TableDataInfo<SysMediaFilesVo> list(SysMediaFilesBo bo, PageQuery pageQuery){
        return sysMediaFilesService.queryPageList(bo, pageQuery);
    }



    /**
     * 获取文件管理详细信息
     *
     * @param fileId 主键
     */
    @SaCheckPermission("system:mediaFiles:query")
    @GetMapping("/{fileId}")
    public R<SysMediaFilesVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long fileId) {
        return R.ok(sysMediaFilesService.queryById(fileId));
    }

    @PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public synchronized R<SysMediaFilesVo> upload(@RequestPart("file") MultipartFile file, @RequestParam(value = "objectName",required = false) String objectName){

        SysMediaFilesVo mediaFiles = sysMediaFilesService.uploadFile(file, objectName);
        if(StringUtils.isNull(mediaFiles)){
            return R.fail("上传图片失败");
        }
        return R.ok(mediaFiles);


    }




    /**
     * 删除文件管理
     *
     * @param fileIds 主键串
     */
    @SaCheckPermission("system:mediaFiles:remove")
    @Log(title = "文件管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fileIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] fileIds) {
        return toAjax(sysMediaFilesService.deleteWithValidByIds(List.of(fileIds), true));
    }



    @PostMapping(value = "/download")
    public void downloadTemplate(HttpServletRequest request, HttpServletResponse response, @RequestParam("bucketName")  String bucketName, @RequestParam("objectName") String objectName){

        sysMediaFilesService.downloadFile(request,response,bucketName, objectName,"");

    }





}
